package com.ruoyi.courseManagement.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.courseManagement.domain.Course;
import com.ruoyi.courseManagement.domain.vo.CourseVO;
import com.ruoyi.courseManagement.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 课程列表Controller
 *
 * @author liao
 * @date 2025-06-09
 */
@RestController
@RequestMapping("/courseManagement/course")
public class CourseController extends BaseController {
    @Autowired
    private CourseService courseService;

    /**
     * 查询课程列表列表
     */
    @PreAuthorize("@ss.hasPermi('courseManagement:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(CourseVO courseVO) {
        startPage();
        List<CourseVO> list = courseService.selectCourseList(courseVO);
        return getDataTable(list);
    }

    /**
     * 导出课程列表列表
     */
    @PreAuthorize("@ss.hasPermi('courseManagement:course:export')")
    @Log(title = "课程列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CourseVO courseVO) {
        List<CourseVO> list = courseService.selectCourseList(courseVO);
        ExcelUtil<CourseVO> util = new ExcelUtil<CourseVO>(CourseVO.class);
        util.exportExcel(response, list, "课程列表数据");
    }

    /**
     * 获取课程列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('courseManagement:course:query')")
    @GetMapping(value = "/{courseId}")
    public AjaxResult getInfo(@PathVariable("courseId") Integer courseId) {
        return success(courseService.selectCourseByCourseId(courseId));
    }

    /**
     * 新增课程列表
     */
    @PreAuthorize("@ss.hasPermi('courseManagement:course:add')")
    @Log(title = "课程列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Course course) {
        return toAjax(courseService.insertCourse(course));
    }

    /**
     * 修改课程列表
     */
    @PreAuthorize("@ss.hasPermi('courseManagement:course:edit')")
    @Log(title = "课程列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Course course) {
        return toAjax(courseService.updateCourse(course));
    }

    /**
     * 删除课程列表
     */
    @PreAuthorize("@ss.hasPermi('courseManagement:course:remove')")
    @Log(title = "课程列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{courseIds}")
    public AjaxResult remove(@PathVariable Integer[] courseIds) {
        return toAjax(courseService.deleteCourseByCourseIds(courseIds));
    }
}
